﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;

namespace Tesla.Content {
    /// <summary>
    /// Abstract loader for loading resources and creating runtime representations of them.
    /// </summary>
    public abstract class ResourceLoader {
        private String _extension;
        private Type _targetType;
        private ContentManager _contentMan;

        /// <summary>
        /// Gets the file extension of the resource to look for.
        /// </summary>
        public String Extension {
            get {
                return _extension;
            }
        }

        /// <summary>
        /// Gets the target type of the object to be returned when the resource is loaded and processed.
        /// </summary>
        public Type TargetType {
            get {
                return _targetType;
            }
        }

        /// <summary>
        /// Gets the content manager that currently references the loader.
        /// </summary>
        public ContentManager ContentManager {
            get {
                return _contentMan;
            }
        }

        /// <summary>
        /// Creates a new resource loader
        /// </summary>
        /// <param name="targetType">Type of resource the loader is responsible for creating</param>
        /// <param name="extension">File extension</param>
        protected ResourceLoader(Type targetType, String extension) {
            _extension = extension;
            _targetType = targetType;
        }

        /// <summary>
        /// Initializes the loader. Called by the content manager when registered, or removed.
        /// </summary>
        /// <param name="manager">Manager that the loader is registered to</param>
        internal void Initialize(ContentManager manager) {
            _contentMan = manager;
            OnInitialize();
        }

        /// <summary>
        /// Called when the loader is initialized by the content manager.
        /// </summary>
        protected virtual void OnInitialize() { }

        /// <summary>
        /// Loads the resource.
        /// </summary>
        /// <param name="resource">Resource to load</param>
        /// <param name="type">Type of resource</param>
        /// <param name="parameters">Loader parameters</param>
        /// <returns>Loaded object</returns>
        public abstract Object Load(IResource resource, Type type, LoaderParameters parameters);
    }

    /// <summary>
    /// Resource loader for strongly-typed resources. This should be subclassed to create loaders for new asset types.
    /// </summary>
    /// <typeparam name="T">Resource target type</typeparam>
    public abstract class ResourceLoader<T> : ResourceLoader {


        /// <summary>
        /// Creates a new resource loader.
        /// </summary>
        /// <param name="extension">File extension of resource to load.</param>
        protected ResourceLoader(String extension) : base(typeof(T), extension) { }

        /// <summary>
        /// Loads the resource.
        /// </summary>
        /// <param name="resource">Resource to load</param>
        /// <param name="type">Type of resource</param>
        /// <param name="parameters">Loader parameters</param>
        /// <returns>Loaded object</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if resource is null.</exception>"
        /// <exception cref="System.ArgumentException">Thrown if the extension of the resource to be opened does not match the loader's extension."</exception>
        /// <exception cref="System.InvalidCastException">Thrown if the object to load's type mismatches the target's type.</exception>
        public override object Load(IResource resource, Type type, LoaderParameters parameters) {
            if(resource == null) {
                throw new ArgumentNullException("Resource cannot be null.");
            }

            if(!resource.Extension.Equals(Extension)) {
                throw new ArgumentException("Invalid file extension, this loader cannot load the specified asset.");
            }

            if(!TargetType.IsAssignableFrom(type)) {
                throw new InvalidCastException(String.Format("Invalid type, cannot load specified type: {0}, expected type: {1}.", type.FullName, TargetType.FullName));
            }

            return Load(resource, parameters);
        }

        /// <summary>
        /// Loads the resource.
        /// </summary>
        /// <param name="resource">Resource to load</param>
        /// <param name="parameters">Loader parameters</param>
        /// <returns>Loaded object</returns>
        public abstract T Load(IResource resource, LoaderParameters parameters);
    }
}
